package com.kye.express.business.personal_my_bill.monthly_balance_bill.detail;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.RequestUser;
import com.kye.model.bean.bill.FinanceInfo;
import com.kye.model.bean.bill.MonthlyBalanceBillBean;
import com.kye.model.bean.bill.request.RequestConfirmBill;
import com.kye.model.bean.bill.request.RequestGetBill;
import com.kye.model.bean.user.User;
import com.kye.model.service.BillService;

import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public interface MonthlyBalanceBillDetailContract {
    interface MonthlyBalanceBillDetailView extends IView, LoadingView, ErrorView {

        void onLoadedData(MonthlyBalanceBillBean monthlyBalanceBillBean);

        void onActionLoading();

        void onCallToFinance(String financePhone);

        void onActionError(String msg);

        void onActionError();

        void onActionLoaded();

        void onConfirmed();
    }

    class MonthlyBalanceBillDetailPresenter extends BasePresenter<MonthlyBalanceBillDetailView> {

        public void getMonthlyBalanceBill(MonthlyBalanceBillBean monthlyBalanceBillBean) {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .flatMap(new Function<User, SingleSource<OpenApiResult<MonthlyBalanceBillBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<MonthlyBalanceBillBean>> apply(User user) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(BillService.class)
                                    .getMonthlyBalanceBill(new RequestGetBill(user.getPhone(), monthlyBalanceBillBean.getBillDate(), monthlyBalanceBillBean.getBillType()));
                        }
                    })
                    .doOnSuccess(new Consumer<OpenApiResult<MonthlyBalanceBillBean>>() {
                        @Override
                        public void accept(OpenApiResult<MonthlyBalanceBillBean> monthlyBalanceBillBeanOpenApiResult) throws Exception {
                            final MonthlyBalanceBillBean data = monthlyBalanceBillBeanOpenApiResult.getData();
                            data.setBillType(monthlyBalanceBillBean.getBillType());
                            data.setBillDate(monthlyBalanceBillBean.getBillDate());

                            monthlyBalanceBillBeanOpenApiResult.setData(data);
                        }
                    })
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<MonthlyBalanceBillBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<MonthlyBalanceBillBean> result, MonthlyBalanceBillBean monthlyBalanceBillBean) {
                            getView().onLoadedData(monthlyBalanceBillBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<MonthlyBalanceBillBean> result, int code, String msg) {
                            getView().onError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }

        public void callToFinance() {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .flatMap(new Function<User, SingleSource<OpenApiResult<FinanceInfo>>>() {
                        @Override
                        public SingleSource<OpenApiResult<FinanceInfo>> apply(User user) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(BillService.class)
                                    .getFinanceInfo(new RequestUser(user.getPhone()));
                        }
                    })
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<FinanceInfo>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onActionLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<FinanceInfo> result, FinanceInfo financeInfo) {
                            getView().onCallToFinance(financeInfo.getFinancePhone());
                        }

                        @Override
                        public void onApiError(OpenApiResult<FinanceInfo> result, int code, String msg) {
                            getView().onActionError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onActionError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onActionLoaded();
                        }
                    });
        }

        public void confirmBill(MonthlyBalanceBillBean monthlyBalanceBillBean) {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .flatMap(new Function<User, SingleSource<OpenApiResult<Object>>>() {
                        @Override
                        public SingleSource<OpenApiResult<Object>> apply(User user) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(BillService.class)
                                    .confirmMonthlyBalanceBill(new RequestConfirmBill(user.getPhone(), monthlyBalanceBillBean.getBillDate()));
                        }
                    })
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<Object>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onActionLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<Object> result, Object o) {
                            getView().onConfirmed();
                        }

                        @Override
                        public void onApiError(OpenApiResult<Object> result, int code, String msg) {
                            getView().onActionError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onActionError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onActionLoaded();
                        }
                    });
        }
    }
}
